package com.lyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyun.bo.EachLoveBo;
import com.lyun.bo.FriendsBo;
import com.lyun.bo.LoveUserBo;
import com.lyun.constants.RabbitMQConstants;
import com.lyun.mapper.EachLoveMapper;
import com.lyun.pojo.EachLove;
import com.lyun.pojo.LoveUser;
import com.lyun.service.EachLoveService;
import com.lyun.vo.LoveCountVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class EachLoveServiceImpl extends ServiceImpl<EachLoveMapper, EachLove> implements EachLoveService {
    @Resource
    private EachLoveMapper eachLoveMapper;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Integer getEachLoveCount(Long selfId) {
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getSelfId, selfId);
        queryWrapper.eq(EachLove::getEachLike, 1);
        return eachLoveMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getLoveCount(Long selfId) {
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getSelfId, selfId);
        return eachLoveMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getFanCount(Long selfId) {
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getUserId, selfId);
        return eachLoveMapper.selectCount(queryWrapper);
    }

    @Override
    public List<Long> getLoveIdsList(Long id) {
        LambdaQueryWrapper<EachLove> eachLoveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eachLoveLambdaQueryWrapper.eq(EachLove::getSelfId, id);
        List<EachLove> eachLoves = eachLoveMapper.selectList(eachLoveLambdaQueryWrapper);

        return eachLoves.stream().map(EachLove::getUserId).collect(Collectors.toList());
    }

    /**
     * 查询该用户id下的互相关注列表的用户id
     *
     * @param bo
     * @return
     */
    @Override
    public List<Long> getEachLoveIdsList(EachLoveBo bo) {
        Page<EachLove> page = new Page<>(bo.getPage(), bo.getPageSize());
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getSelfId, bo.getUserId());
        queryWrapper.eq(EachLove::getEachLike, 1);
        eachLoveMapper.selectPage(page, queryWrapper);
        List<EachLove> eachLoveList = page.getRecords();

        return eachLoveList.stream().map(EachLove::getUserId).collect(Collectors.toList());
    }

    @Override
    public List<Long> getFanIdsList(Long id) {
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getUserId, id);
        List<EachLove> eachLoves = eachLoveMapper.selectList(queryWrapper);

        return eachLoves.stream().map(EachLove::getSelfId).collect(Collectors.toList());
    }

    @Override
    public boolean getAlreadyLoveByUserIdAndToId(long id, Long uid) {
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getSelfId, id);
        queryWrapper.eq(EachLove::getUserId, uid);

        return eachLoveMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public void insertOrUpdate(Long id, Long toId) {

        rabbitTemplate.convertAndSend(RabbitMQConstants.LOVE_USER_EXCHANGE_NAME, RabbitMQConstants.LOVE_USER_INSERT_KEY, new LoveUserBo(id, toId));

    }

    @Override
    public void removeBySelfIdAndUserId(Long id, Long toId) {

        rabbitTemplate.convertAndSend(RabbitMQConstants.LOVE_USER_EXCHANGE_NAME, RabbitMQConstants.LOVE_USER_DELETE_KEY, new LoveUserBo(id, toId));

    }

    /**
     * 查询该用户id下的关注信息
     *
     * @param page     当前页
     * @param pageSize 页面大小
     * @param userId   用户id
     * @return
     */
    @Override
    public Page<EachLove> selectLovePage(Integer page, Integer pageSize, Long userId) {
        Page<EachLove> eachLovePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(EachLove::getSelfId, userId);
        return eachLoveMapper.selectPage(eachLovePage, queryWrapper);
    }

    /**
     * 查询用户粉丝信息
     *
     * @param page     当前页
     * @param pageSize 页面大小
     * @param userId   用户id
     */
    @Override
    public Page<EachLove> selectFansPage(Integer page, Integer pageSize, Long userId) {
        Page<EachLove> eachLovePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(EachLove::getUserId, userId);
        return eachLoveMapper.selectPage(eachLovePage, queryWrapper);
    }

    /**
     * 根据分页条件，获取粉丝id列表
     *
     * @param bo
     * @return
     */
    @Override
    public List<Long> getFanIdsListWithPage(FriendsBo bo) {
        Page<EachLove> page = new Page<>(bo.getPage(), bo.getPageSize());
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EachLove::getUserId, bo.getUserId());
        eachLoveMapper.selectPage(page, queryWrapper);
        List<EachLove> records = page.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return Collections.emptyList();
        }

        return records.stream().map(EachLove::getSelfId).collect(Collectors.toList());
    }

    /**
     * 查询喜欢、粉丝数
     * @param userId 用户id
     * @return
     */
    @Override
    public LoveCountVo selectCount(Long userId) {
        LoveCountVo vo = new LoveCountVo();
        LambdaQueryWrapper<EachLove> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(EachLove::getSelfId, userId);
        Integer loveCount = this.count(queryWrapper);
        vo.setLoveCount(loveCount);

        queryWrapper.clear();
        queryWrapper.eq(EachLove::getUserId, userId);
        Integer fanCount = this.count(queryWrapper);
        vo.setFanCount(fanCount);

        queryWrapper.eq(EachLove::getEachLike, 1);
        Integer eachLoveCount = this.count(queryWrapper);
        vo.setEachLoveCount(eachLoveCount);
        return vo;
    }
}
